﻿using System.Xml.Linq;

namespace TDMS.Net.Internals;

internal static class ExceptionBuilder
{
    public static void CheckApiStatus(int status)
    {
        if ((int)EDDCError.DDC_NoError == status) return;
        var errorBuffer = NiLibDDC.DDC_GetLibraryErrorDescription(status);
        var errorString = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(errorBuffer);
        throw new TdmsException(errorString!, status);
    }

    public static bool CheckIfPropertyContainData(int status)
    {
        if ((int)EDDCError.DDC_PropertyDoesNotContainData == status)
        {
            return false;
        }

        CheckApiStatus(status);

        return true;
    }

    public static void ThrowPropertyDataTypeMismatch(string name)
    {
        throw new TdmsException($"property data type mismatch, name={name}", (int)EDDCError.DDC_PropertyDataTypeMismatch);
    }
}